package cn.itcast.nems.listener.order;

import cn.hutool.core.lang.Assert;
import cn.itcast.nems.common.tenant.TenantInfo;
import cn.itcast.nems.common.tenant.TenantInfoHolder;
import cn.itcast.nems.listener.NemsMessageListener;
import cn.itcast.nems.order.dto.message.OrderPaidMessageDTO;
import cn.itcast.nems.studentrecord.dto.OrderPaidDTO;
import cn.itcast.nems.studentrecord.service.studentrecord.StudentRecordOrderService;
import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 订单支付消息监听处理逻辑
 * </p>
 *
 * @author zhouwei
 * @since 2023/12/7 2:07 PM
 **/
@Component
@RequiredArgsConstructor
public class OrderPaidMessageListener implements NemsMessageListener {
    private final static String TagName  =  "TAG_NEMS_ORDER_PAID" ;
    private final StudentRecordOrderService studentRecordOrderService ;

    @Override
    public String getTagName() {
        return TagName;
    }

    @Override
    public void processMessage(String body) {
        OrderPaidMessageDTO paidMessageDTO = JSON.parseObject(body, OrderPaidMessageDTO.class);
        Assert.notNull(paidMessageDTO, "【消息监听】tag: TAG_NEMS_ORDER_PAID, 消息体中业务数据实体为空");
        Assert.notBlank(paidMessageDTO.getOrderId(), "【消息监听】tag: TAG_NEMS_ORDER_PAID,业务订单ID为空");
        OrderPaidDTO orderPaid = new OrderPaidDTO();
        BeanUtils.copyProperties(paidMessageDTO, orderPaid);
        orderPaid.setPaymentType(paidMessageDTO.getJournalType());
        TenantInfoHolder.setTenantInfo(new TenantInfo(paidMessageDTO.getTenantId(), paidMessageDTO.getSubtenantId()));
        studentRecordOrderService.orderPaid(orderPaid);
    }
}
